package com.brkj.course;

import java.io.File;

import com.brkj.main.R;

import android.R.integer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.brkj.util.ConstAnts;
import com.brkj.util.view.MyVideoView;
import com.dgl.sdk.util.FileCache;;

public class ShowVideoActivity extends Activity implements
		MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {
	public static final String TAG = "VideoPlayer";
	private MyVideoView mVideoView;
	private Uri mUri;
	private int mPositionWhenPaused = -1;
	private MediaController mMediaController;
	private View loading;

	private File localFile = null;
	private float coastTime = 0.0f;
	private boolean isDecodeing = false;
	
	private RecordTime recordTime;
	private ImageView videobg;
	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				mUri=Uri.parse((String)msg.obj);
				
				mMediaController = new MediaController(ShowVideoActivity.this);// 设置MediaController
				mVideoView.setMediaController(mMediaController);
				
				mVideoView.setOnPreparedListener(new OnPreparedListener() {
					
					@Override
					public void onPrepared(MediaPlayer mp) {
					}
				});
//				ToastShow.showToast(ShowVideoActivity.this, "耗时：" + Float.toString(coastTime));
				mVideoView.setVideoURI(mUri);
				mVideoView.start();
				break;
			
			case 2:
				break;
			default:
				break;
			}

		}
		
	};
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 无title
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		  //不显示系统的标题栏          
        getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,
                              WindowManager.LayoutParams.FLAG_FULLSCREEN );

		setContentView(R.layout.videoview);
		Intent intent=getIntent();
		String title=intent.getExtras().getString("videotitle");
		overridePendingTransition(R.anim.anim_right_enter,
				R.anim.anim_left_exit);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		mVideoView = (MyVideoView) findViewById(R.id.videoview);
		videobg = (ImageView) findViewById(R.id.videobg);
		loading = (View) findViewById(R.id.loading);
//		mUri=Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.id.bottom_chaptername) ;
		final String url=getIntent().getStringExtra("VideoAdress");
		
		
		recordTime = new RecordTime(this);
		
		mVideoView.setRecordTime(recordTime);
		
		FileCache fileCache =  new FileCache(this, ConstAnts.DOWNLOAD_COURSE__DIR);
		
		String localFile = fileCache.getAbsolutePathByUrl(url);
		
		if(new File(localFile).exists()){
			playVideo(localFile);
		}else{
			playVideo(url);
		}
		
		

	}

	private void playVideo(String url){
		mUri=Uri.parse(url);
		
		mMediaController = new MediaController(this);// 设置MediaController
		mVideoView.setMediaController(mMediaController);
		
		mVideoView.setOnPreparedListener(new OnPreparedListener() {
			
			@Override
			public void onPrepared(MediaPlayer mp) {
				videobg.setVisibility(View.GONE);
				loading.setVisibility(View.GONE);
			}
		});
		
		mVideoView.setVideoURI(mUri);
		mVideoView.start();
		
		mVideoView.setOnCompletionListener(new OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				
				recordTime.end();
				
			}
		});
		
	}


	// 监听MediaPlayer上报的错误信息
	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		// TODO Auto-generated method stub
		mp.reset();
		return false;
	}

	// Video播完的时候得到通知
	@Override
	public void onCompletion(MediaPlayer mp) {
		finish();
	}

	// 开始
	@Override
	public void onStart() {
		// Play Video
//		mVideoView.setVideoURI(mUri);
//		mVideoView.start();
		super.onStart();
	}

	// 暂停
	@Override
	public void onPause() {
		// Stop video when the activity is pause.
		mPositionWhenPaused = mVideoView.getCurrentPosition();
		mVideoView.stopPlayback();
		
		super.onPause();
	}

	@Override
	public void onResume() {
		// Resume video player
		if (mPositionWhenPaused >= 0) {
			mVideoView.seekTo(mPositionWhenPaused);
			mPositionWhenPaused = -1;
		}
		super.onResume();
	}

	@Override
	public void finish() {
		mVideoView.stopPlayback();
		recordTime.recodeTime(getIntent().getStringExtra("CourseID"), 
				Integer.toString(recordTime.getTotalTime()));
		super.finish();
	}
	

//    @Override
//public void onBackPressed() { 
//    //实现Home键效果 
//    //super.onBackPressed();这句话一定要注掉,不然又去调用默认的back处理方式了 
//    Intent i= new Intent(Intent.ACTION_MAIN); 
//    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
//    i.addCategory(Intent.CATEGORY_HOME); 
//    startActivity(i);  
//}
}